package org.ponly.cta4j.factory;

import java.awt.image.BufferedImage;
import java.util.Locale;
import org.ponly.cta4j.Captcha;
import org.ponly.cta4j.CaptchaFactory;
import org.ponly.cta4j.drawing.GeneralPainter;
import org.ponly.cta4j.drawing.Painter;

/**
 * 抽象的验证码工厂.
 *
 * @author vacoor
 * @since 1.0
 */
public abstract class AbstractCaptchaFactory implements CaptchaFactory {
    /**
     * Painter.
     */
    private Painter painter;

    /**
     * 验证码宽度.
     */
    private int width = 200;

    /**
     * 验证码高度.
     */
    private int height = 70;

    /**
     * 创建一个默认工厂.
     */
    public AbstractCaptchaFactory() {
        painter = new GeneralPainter();
    }

    /**
     * 使用给定 Painter 创建一个工厂.
     *
     * @param painter Painter
     */
    public AbstractCaptchaFactory(final Painter painter) {
        this.painter = painter;
    }

    /**
     * 使用给定 Painter 创建一个验证码宽高为 width, height 的工厂.
     *
     * @param width   验证码宽度
     * @param height  验证码高度
     * @param painter Painter
     */
    public AbstractCaptchaFactory(final int width, final int height, final Painter painter) {
        this.width = width;
        this.height = height;
        this.painter = painter;
    }

    @Override
    public abstract Captcha getCaptcha();

    /**
     * {@inheritDoc}
     */
    @Override
    public abstract Captcha getCaptcha(final Locale locale);

    /**
     * 根据当前配置创建画布.
     *
     * @return 新创建的画布
     */
    protected BufferedImage createCanvas() {
        return new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    }

    /* ***************************************
     *           GETTER / SETTER
     * ************************************* */

    /**
     * @return 当前使用的 Painter
     */
    public Painter getPainter() {
        return painter;
    }

    /**
     * 设置当前 Painter.
     *
     * @param painter Painter
     */
    public void setPainter(final Painter painter) {
        this.painter = painter;
    }

    /**
     * @return 当前验证码的宽度
     */
    public int getWidth() {
        return width;
    }

    /**
     * 设置验证码的宽度.
     *
     * @param width 新宽度
     */
    public void setWidth(final int width) {
        this.width = width;
    }

    /**
     * @return 当前验证码的高度
     */
    public int getHeight() {
        return height;
    }

    /**
     * 设置验证码的高度.
     *
     * @param height 新高度
     */
    public void setHeight(final int height) {
        this.height = height;
    }
}
